package cn.new18.testactivitytask.base;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by RaoWei on 2016/12/20 17:15.
 */

public abstract class MyLazyFragment extends Fragment {

    private static final String TAG = "MyLazyFragment";
    private boolean enableSetUserVisibleHint = false;
    private boolean isPagePrepared = false;
    //private boolean isOnHiddenChangedRun = false;
    private boolean isSetUserVisibleHintRun = false;
    //private boolean isHidden = false;

    @Override
    public void onHiddenChanged(boolean hidden) {
        Log.e(TAG, "onHiddenChanged : hidden = " + hidden);
        super.onHiddenChanged(hidden);
        //isOnHiddenChangedRun = true;
        //isHidden = hidden;
        if (hidden) {
            pageInvisibleToUser();
        } else {
            pageVisibleToUser();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        Log.e(TAG, "setUserVisibleHint : isVisibleToUser = " + isVisibleToUser);
        super.setUserVisibleHint(isVisibleToUser);
        isSetUserVisibleHintRun = true;
        if (!enableSetUserVisibleHint) {
            //enableSetUserVisibleHint = false;
            Log.e(TAG, "onResume还没走");
            return;
        }
        if (isVisibleToUser) {
            pageVisibleToUser();
        } else {
            pageInvisibleToUser();
        }
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        Log.e(TAG, "onAttachFragment");
        super.onAttachFragment(childFragment);
    }

    @Override
    public void onAttach(Context context) {
        Log.e(TAG, "onAttach");
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView");
        return onCreatePage(inflater, container, savedInstanceState);
    }

    protected abstract View onCreatePage(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onViewStateRestored");
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.e(TAG, "onStart");
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.e(TAG, "onResume");
        super.onResume();
        //没有准备，先准备
        if (!isPagePrepared) {
            pagePreparedToUser();
        }
        //准备好之后，区分fragment显示隐藏的方式：viewpager，show/hide
        if (isSetUserVisibleHintRun) {  //viewpager
            enableSetUserVisibleHint = true;
            if (getUserVisibleHint()) {
                pageVisibleToUser();
            }
        } else {  //show or hide
            //isOnHiddenChangedRun = true;
            if (!isHidden()) {
                pageVisibleToUser();
            }
        }
    }

    @Override
    public void onPause() {
        Log.e(TAG, "onPause");
        super.onPause();
        if (isSetUserVisibleHintRun) {
            enableSetUserVisibleHint = false;
        }
        pageInvisibleToUser();
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        isPagePrepared = false;
    }

    @Override
    public void onDetach() {
        Log.e(TAG, "onDetach");
        super.onDetach();
    }

    /**
     * 每次fragment对用户可见时，会被调用
     */
    protected void pageVisibleToUser() {}

    /**
     * 每次fragment对用户不可见时，被调用
     */
    protected void pageInvisibleToUser() {

    }

    /**
     * viewpager预加载时会被调用，如果一个页面直接接展示在用户面前，即第一时间走了pageVisibleToUser()，该方法也会被提前调用一次
     */
    protected void pagePreparedToUser() {
        isPagePrepared = true;
    }
}
